package com.zllkj.mm.module.home.fragments;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.zllkj.mm.R;
import com.zllkj.mm.api.network.RetrofitHelper;
import com.zllkj.mm.api.network.RxHelper;
import com.zllkj.mm.api.network.subscriber.BaseString;
import com.zllkj.mm.api.network.subscriber.ProgressSubscriber;
import com.zllkj.mm.base.BaseTitleFragment;
import com.zllkj.mm.common.L;
import com.zllkj.mm.model.entity.UserSelfVo;
import com.zllkj.mm.widgets.AddressDialog;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 我的小店开通申请
 * Created by leon_ok.
 */

public class ShopApplyFragment extends BaseTitleFragment {

    @BindView(R.id.tvrRealStatus)
    TextView tvrRealStatus;
    @BindView(R.id.etName)
    EditText etShopName;
    @BindView(R.id.etChanPin)
    EditText etChanPin;
    @BindView(R.id.etBossName)
    EditText etBossName;
    @BindView(R.id.etPhone)
    EditText etPhone;
    @BindView(R.id.tvSSQ)
    TextView tvSSQ;
    @BindView(R.id.etAddress)
    EditText etAddress;
    @BindView(R.id.confirm1)
    TextView confirm1;

    private UserSelfVo userInfo;

    public static ShopApplyFragment newInstance() {
        return new ShopApplyFragment();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_shopapply;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setCommonTitle("开通小店");
        userInfo= Hawk.get(L.userInfo);
        if (userInfo.realStatus==0){
            tvrRealStatus.setText("未认证");
        }else {
            tvrRealStatus.setText("已认证");
        }
    }

    /**
     * 省
     */
    private String mProvince = "";
    /**
     * 市
     */
    private String mCity = "";
    /**
     * 区
     */
    private String mArea = "";

    @OnClick({R.id.tvrRealStatus, R.id.tvSSQ, R.id.confirm1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvrRealStatus:
                break;
            case R.id.tvSSQ:
                new AddressDialog.Builder(getContext())
                        //.setTitle("选择地区")
                        // 设置默认省份
//                        .setProvince(mProvince)
                        // 设置默认城市（必须要先设置默认省份）
//                        .setCity(mCity)
                        // 不选择县级区域
                        //.setIgnoreArea()
                        .setListener((dialog, province, city, area) -> {
                            String address = province + city + area;
                            mProvince = province;
                            mCity = city;
                            mArea = area;
                            tvSSQ.setText(province + " " + city + " " + area);
                        })
                        .show();
                break;
            case R.id.confirm1:
                if (TextUtils.isEmpty(etShopName.getText().toString())) {
                    L.showToast("请把信息填写完整");
                    return;
                }
                if (TextUtils.isEmpty(etChanPin.getText().toString())) {
                    L.showToast("请把主营产品填写完整");
                    return;
                }
                if (TextUtils.isEmpty(etBossName.getText().toString())) {
                    L.showToast("请把店主姓名填写完整");
                    return;
                }
                if (TextUtils.isEmpty(mProvince)) {
                    L.showToast("请选择地区");
                    return;
                }
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    L.showToast("请把信息填写完整");
                    return;
                }
                if (TextUtils.isEmpty(etAddress.getText().toString())) {
                    L.showToast("请把信息填写完整");
                    return;
                }
                addSubscribe(RetrofitHelper.getInstance().getApi()
                        .shopApply(L.sign(new HashMap() {{
                            put("addressInfo",etAddress.getText().toString());
                            put("businessScope",etChanPin.getText().toString());
                            put("city",mCity);
                            put("district",mArea);
                            put("province",mProvince);
                            put("shopName",etShopName.getText().toString());
                            put("shopPhone",etPhone.getText().toString());
                            put("shopkeeperName",etBossName.getText().toString());
                        }}))
                        .compose(RxHelper.<BaseString>defaultHandleResult())
                        .subscribe(new ProgressSubscriber<BaseString>(getContext()) {
                            @Override
                            protected void onSucc(BaseString o) {
//                                addFragment(Auth2Fragment_tjqy3.newInstance());
                                L.showToast("已提交资料审核，请耐心等待");
                                removeFragment();
                            }
                        })
                );
                break;
        }
    }
}

